/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.averages;

import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import systole.domain.analysis.results.ParametersKind;
import systole.domain.analysis.results.averages.AverageByDecadeFemale;
import systole.domain.analysis.results.averages.AverageByDecadeMale;
import systole.domain.analysis.results.averages.Averages;

/**
 *
 * @author jmj
 */
public class AveragesTableModel extends DefaultTableModel {

    /**
     *
     */
    private static final long serialVersionUID = -1189826059669285371L;
    private DecimalFormat format = new DecimalFormat("###0.00");

    /**
     * @param averages
     */
    public AveragesTableModel(Averages averages) {
        super();
        this.addColumn("Descripción");
        this.addColumn("Década");
        this.addColumn("Sexo");
        this.addColumn("Muestras");
        this.addColumn("Promedio");
        this.addColumn("Int. de Confianza");
        this.loadTable(averages);
    }

    @SuppressWarnings("unchecked")
    private void loadTable(Averages averages) {
        Iterator<AverageByDecadeFemale> itAVGF = averages.getFemaleAverages().iterator();

        while (itAVGF.hasNext()) {
            AverageByDecadeFemale f = itAVGF.next();
            // add AOD
            Vector rowData = new Vector();
            rowData.add(ParametersKind.AOD);
            rowData.add(f.getDecade());
            rowData.add("F");
            rowData.add(f.getSamples());
            rowData.add(this.format.format((f.getAverageAOD() != null) ? f.getAverageAOD().getAvg() : 0));
            rowData.add(this.format.format((f.getAverageAOD() != null) ? f.getAverageAOD().getTrustInterval() : 0));
            this.addRow(rowData);
            // add AOS
            rowData = new Vector();
            rowData.add(ParametersKind.AOS);
            rowData.add(f.getDecade());
            rowData.add("F");
            rowData.add(f.getSamples());
            rowData.add(this.format.format((f.getAverageAOS() != null) ? f.getAverageAOS().getAvg() : 0));
            rowData.add(this.format.format((f.getAverageAOS() != null) ? f.getAverageAOS().getTrustInterval() : 0));
            this.addRow(rowData);
            // add IAR
            rowData = new Vector();
            rowData.add(ParametersKind.IAR);
            rowData.add(f.getDecade());
            rowData.add("F");
            rowData.add(f.getSamples());
            rowData.add(this.format.format((f.getAverageIAR() != null) ? f.getAverageIAR().getAvg() : 0));
            rowData.add(this.format.format((f.getAverageIAR() != null) ? f.getAverageIAR().getTrustInterval() : 0));
            this.addRow(rowData);
        }


        Iterator<AverageByDecadeMale> itAVGM = averages.getMaleAverages().iterator();

        while (itAVGM.hasNext()) {
            AverageByDecadeMale m = itAVGM.next();
            // add AOD
            Vector rowData = new Vector();
            rowData.add(ParametersKind.AOD);
            rowData.add(m.getDecade());
            rowData.add("M");
            rowData.add(m.getSamples());
            rowData.add(this.format.format((m.getAverageAOD() != null) ? m.getAverageAOD().getAvg() : 0));
            rowData.add(this.format.format((m.getAverageAOD() != null) ? m.getAverageAOD().getTrustInterval() : 0));
            this.addRow(rowData);
            // add AOS
            rowData = new Vector();
            rowData.add(ParametersKind.AOS);
            rowData.add(m.getDecade());
            rowData.add("M");
            rowData.add(m.getSamples());
            rowData.add(this.format.format((m.getAverageAOS() != null) ? m.getAverageAOS().getAvg() : 0));
            rowData.add(this.format.format((m.getAverageAOS() != null) ? m.getAverageAOS().getTrustInterval() : 0));
            this.addRow(rowData);
            // add IAR
            rowData = new Vector();
            rowData.add(ParametersKind.IAR);
            rowData.add(m.getDecade());
            rowData.add("M");
            rowData.add(m.getSamples());
            rowData.add(this.format.format((m.getAverageIAR() != null) ? m.getAverageIAR().getAvg() : 0));
            rowData.add(this.format.format((m.getAverageIAR() != null) ? m.getAverageIAR().getTrustInterval() : 0));
            this.addRow(rowData);
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
}
